package com.avocado.base.validator.user;


import com.avocado.base.repository.UserRepository;
import lombok.extern.slf4j.Slf4j;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

@Slf4j
public class UserAddNameValidator implements ConstraintValidator<UserAddNameValidation, String> {


    private final UserRepository userRepository;

    public UserAddNameValidator(UserRepository userRepository) {

        this.userRepository = userRepository;
    }


    @Override
    public void initialize(UserAddNameValidation constraintAnnotation) {

    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {

        try {

            long count=userRepository.getCountByName(value);

            return count<1L;

        } catch (Exception ex) {
            log.error(ex.getLocalizedMessage());
        }
        return false;
    }
}
